package cn.gary.oaserver.config.utils;

import cn.gary.oaserver.entity.TMenu;
import cn.gary.oaserver.mapper.TMenuMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: 邓必宏
 * @title:
 * @Demo:
 * @DateTime: 2024/3/30
 */

@Component
public class MenuStreamUtil {

    @Autowired
    TMenuMapper menuMapper;

    public List<TMenu> getAllMenusByRidAndMenuId(Integer rid) {
        List<TMenu> menus = menuMapper.getMenusByRoleId ( rid );
        List<TMenu> collect = new ArrayList<> ( );
        if (menus != null && menus.size ( ) > 0) {
            collect = menus.stream ( )
                    .filter ( menu -> menu.getParentId ( ) != null && menu.getParentId ( ) == 1 )
                    .map ( menu -> {
                        List<TMenu> children = getChildren ( menu.getId ( ), menus );
                        menu.setChildren ( children.size ( ) > 0 ? children : null );
                        return menu;
                    } ).collect ( Collectors.toList ( ) );
        }
        return collect;
    }

    public List<TMenu> getAllMenus() {
        List<TMenu> menus = menuMapper.selectList ( null );
        List<TMenu> collect = null;
        if (menus != null && menus.size ( ) > 0) {
            collect = menus.stream ( )
                    .filter ( menu -> menu.getParentId ( ) != null && menu.getParentId ( ) == 1 )
                    .map ( menu -> {
                        List<TMenu> children = getChildren ( menu.getId ( ), menus );
                        menu.setChildren ( children.size ( ) > 0 ? children : null );
                        return menu;
                    } ).collect ( Collectors.toList ( ) );
        }
        return collect;
    }

    public List<TMenu> getMenusByAllMenus(List<TMenu> allMenus) {
        List<TMenu> menus = allMenus;
        List<TMenu> collect = null;
        if (menus != null && menus.size ( ) > 0) {
            collect = menus.stream ( )
                    .filter ( menu -> menu.getParentId ( ) != null && menu.getParentId ( ) == 1 )
                    .map ( menu -> {
                        List<TMenu> children = getChildren ( menu.getId ( ), menus );
                        menu.setChildren ( children.size ( ) > 0 ? children : null );
                        return menu;
                    } ).collect ( Collectors.toList ( ) );
        }
        return collect;
    }

    private List<TMenu> getChildren(Integer id, List<TMenu> menus) {
        return menus.stream ( )
                .filter ( menu -> menu.getParentId ( ) != null && menu.getParentId ( ).intValue ( ) == id.intValue ( ) )
                .map ( menu -> {
                    List<TMenu> children = getChildren ( menu.getId ( ), menus );
                    menu.setChildren ( children.size ( ) > 0 ? children : null );
                    return menu;
                } )
                .collect ( Collectors.toList ( ) );
    }
}
